package cn.iocoder.yudao.module.book.controller.admin.author;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.author.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.author.AuthorDO;
import cn.iocoder.yudao.module.book.service.author.AuthorService;

@Tag(name = "图书管理系统 - 作者")
@RestController
@RequestMapping("/book/author")
@Validated
public class AuthorController {

    @Resource
    private AuthorService authorService;

    @PostMapping("/create")
    @Operation(summary = "创建作者")
    @PreAuthorize("@ss.hasPermission('book:author:create')")
    public CommonResult<Long> createAuthor(@Valid @RequestBody AuthorSaveReqVO createReqVO) {
        return success(authorService.createAuthor(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新作者")
    @PreAuthorize("@ss.hasPermission('book:author:update')")
    public CommonResult<Boolean> updateAuthor(@Valid @RequestBody AuthorSaveReqVO updateReqVO) {
        authorService.updateAuthor(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除作者")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:author:delete')")
    public CommonResult<Boolean> deleteAuthor(@RequestParam("id") Long id) {
        authorService.deleteAuthor(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得作者")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:author:query')")
    public CommonResult<AuthorRespVO> getAuthor(@RequestParam("id") Long id) {
        AuthorDO author = authorService.getAuthor(id);
        return success(BeanUtils.toBean(author, AuthorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得作者分页")
    @PreAuthorize("@ss.hasPermission('book:author:query')")
    public CommonResult<PageResult<AuthorRespVO>> getAuthorPage(@Valid AuthorPageReqVO pageReqVO) {
        PageResult<AuthorDO> pageResult = authorService.getAuthorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AuthorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出作者 Excel")
    @PreAuthorize("@ss.hasPermission('book:author:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAuthorExcel(@Valid AuthorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuthorDO> list = authorService.getAuthorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "作者.xls", "数据", AuthorRespVO.class,
                        BeanUtils.toBean(list, AuthorRespVO.class));
    }

}